package com.yoyo.yyadsdk.bean;

import com.yoyo.yyadsdk.bean.dao.AdvScreenEntity;
import com.yoyo.yyadsdk.bean.dao.AppEntity;
import com.yoyo.yyadsdk.bean.dao.ConfigEntity;
import com.yoyo.yyadsdk.bean.dao.MonitorTimeEntity;
import com.yoyo.yyadsdk.bean.dao.PullEntity;
import com.yoyo.yyadsdk.bean.dao.PushEntity;
import com.yoyo.yyadsdk.bean.dao.RuleListEntity;
import com.yoyo.yyadsdk.bean.dao.ShortcutEntity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PullBean {
    /**
     * appList : [{"id":1,"tpkg":"com.feng.waveview","period":24,"duration":5,"livetime":-1,"isCloseApp":0,"isAutoInst":0,"appUrl":"https://yoyo-wallpaper.oss-cn-shenzhen.aliyuncs.com/wave1.0.apk"},{"id":2,"tpkg":"com.feng.waveview","period":24,"duration":1,"livetime":-1,"isCloseApp":0,"isAutoInst":0,"appUrl":null}]
     * ruleList : [{"id":4,"type":0,"pkg":"com.jk.dao","model":null,"channel":"11","country":"11","city":"11","imei":"1111","userActive":null,"ruleName":"规则3"},{"id":5,"type":0,"pkg":"11,22","model":null,"channel":"11,22","country":"11,22","city":"11","imei":"22","userActive":null,"ruleName":"规则4"},{"id":6,"type":0,"pkg":"11","model":"xiaomi","channel":"111","country":"22","city":"333","imei":"444","userActive":"11:222","ruleName":"规则3"},{"id":7,"type":1,"pkg":"2,13","model":"3,12","channel":"4,12","country":"5,6","city":"7","imei":"8","userActive":"1:4,1:2","ruleName":"1"},{"id":8,"type":1,"pkg":"1,1","model":"4,4","channel":"7,7","country":"10,10","city":"13,13","imei":"16,16","userActive":"3:4","ruleName":"5555"}]
     * monitorList : [{"id":1,"type":1,"startHour":0,"startMinute":5,"endHour":23,"endMinute":55},{"id":2,"type":2,"startHour":1,"startMinute":2,"endHour":3,"endMinute":4}]
     * configList : {"id":1,"autoDelayTime":30,"interval":1,"getInterval":24}
     * pullList : [{"id":1,"type":1,"tpkg":"com.feng.waveview","action":"will://link/testId","inactivity":1,"frequency":100,"pullTimes":1,"priority":100,"startTime":1558057960000,"endTime":1558057962000,"duringStartTime":"2200","duringEndTime":"2359","isBackHome":1,"ruleId":2,"monitorTimeId":1,"allowDiscover":null},{"id":2,"type":1,"tpkg":"com.feng.waveview","action":"2342","inactivity":1,"frequency":2,"pullTimes":3,"priority":100,"startTime":1557196181000,"endTime":1558060183000,"duringStartTime":"0900","duringEndTime":"0930","isBackHome":1,"ruleId":2,"monitorTimeId":1,"allowDiscover":null},{"id":3,"type":1,"tpkg":"com.feng.waveview","action":"will://link/testId","inactivity":1,"frequency":1,"pullTimes":1,"priority":100,"startTime":1558062047000,"endTime":1558062049000,"duringStartTime":"1030","duringEndTime":"1000","isBackHome":1,"ruleId":2,"monitorTimeId":2,"allowDiscover":null}]
     */
    private ConfigBean configList;
    private List<AppEntity> appList;
    private List<RuleListBean> ruleList;
    private List<MonitorTimeEntity> monitorList;
    private List<PullEntity> pullList;
    private List<PushEntity> pushList;
    private List<ShortcutEntity> shortcutList;
    private List<AdvScreenEntity> advScreenList;
    private String addr;
    private ConfigEntity c;
    private List<RuleListEntity> r;

    public ConfigBean getConfigList() {
        return configList;
    }

    public ConfigEntity getConfig() {
        if (c != null) return c;
        c = new ConfigEntity();
        c.setGetInterval(configList.getGetInterval());
        c.setInterval(configList.getInterval());
        c.setAdInterval(configList.getAdInterval());
        c.setAutoDelayTime(configList.getAutoDelayTime());
        c.setLastUpdateTime(configList.getLastUpdateTime());
        c.setLockAdvMaxCount(configList.getLockAdvMaxCount());
        c.setShortcutMaxCount(configList.getShortcutMaxCount());
        c.setAdPeriod(configList.getAdPeriod());
//        c.setPushShowTime(convertToListString(configList.getPushShowTime()));
//        c.setAdShowTime(convertToListString(configList.getAdShowTime()));
        return c;
    }

    public void setConfigList(ConfigBean configList) {
        this.configList = configList;
    }

    public List<AppEntity> getAppList() {
        return appList;
    }

    public void setAppList(List<AppEntity> appList) {
        this.appList = appList;
    }

    public List<RuleListBean> getRule() {
        return ruleList;
    }

    public List<RuleListEntity> getRuleList() {
        if (r != null) return r;
        r = new ArrayList<>();
        for (RuleListBean rule : ruleList) {
            RuleListEntity re = new RuleListEntity();
            re.setRuleId(rule.getId());
            re.setType(rule.getType());
            re.setCountry(convertToListString(rule.getCountry()));
            re.setPkg(convertToListString(rule.getPkg()));
            re.setModel(convertToListString(rule.getModel()));
            re.setImei(convertToListString(rule.getImei()));
            re.setCity(convertToListString(rule.getCity()));
            re.setChannel(convertToListString(rule.getChannel()));
            re.setUserActive(convertToEntityProperty(rule.getUserActive()));
            r.add(re);
        }
        return r;
    }

    private List<String> convertToListString(String databaseValue) {
        return databaseValue == null || databaseValue.isEmpty() ? null : Arrays.asList(databaseValue.split(","));

    }

    private List<UserActive> convertToEntityProperty(String databaseValue) {
        if (databaseValue == null || databaseValue.equals(""))
            return null;
        List<String> list = Arrays.asList(databaseValue.split(","));
        if (list.size() > 0) {
            List<UserActive> listTemp = new ArrayList<>();
            for (String s : list) {
                try {
                    String[] split = s.split(":");
                    listTemp.add(new UserActive(split[0], Integer.parseInt(split[1])));
                } catch (Exception ignored) {

                }
            }
            return listTemp;
        }
        return null;
    }

    public void setRuleList(List<RuleListBean> ruleList) {
        this.ruleList = ruleList;
    }

    public List<MonitorTimeEntity> getMonitorList() {
        return monitorList;
    }

    public void setMonitorList(List<MonitorTimeEntity> monitorList) {
        this.monitorList = monitorList;
    }

    public List<PullEntity> getPullList() {
        return pullList;
    }

    public void setPullList(List<PullEntity> pullList) {
        this.pullList = pullList;
    }

    public List<PushEntity> getPushList() {
        return pushList;
    }

    public void setPushList(List<PushEntity> pushList) {
        this.pushList = pushList;
    }

    public List<ShortcutEntity> getShortcutList() {
        return shortcutList;
    }

    public void setShortcutList(List<ShortcutEntity> shortcutList) {
        this.shortcutList = shortcutList;
    }

    public List<AdvScreenEntity> getAdvScreenList() {
        return advScreenList;
    }

    public void setAdvScreenList(List<AdvScreenEntity> advScreenList) {
        this.advScreenList = advScreenList;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }
}
